package com.example.user.newsongpaly;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class FragmentList extends Fragment {
    SongJsong songJsong;
    List<String> list = new ArrayList<>();
    SongListAdpter songListAdpter;
    @BindView(R.id.listView)
    ListView listView;
    Unbinder unbinder;
    final static String TAG = "FragmentList";
    public FragmentList() {
        // Required empty public constructor
    }

    public static FragmentList newInstance() {
        FragmentList fragment = new FragmentList();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        songListAdpter = new SongListAdpter(getContext(),list);
        listView.setAdapter(songListAdpter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(),MyService.class);
                intent.putExtra("Url",ListContentlist.getlistContentlist().getContentlistBeans().get(i).getM4a());
                getActivity().startService(intent);
                ListContentlist.getlistContentlist().setitem(i);
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public void init() {
        if (ListContentlist.getlistContentlist().getContentlistBeanSize()<=0){
            OkGo.<String>post("http://route.showapi.com/213-1")
                    .tag(this)
                    .params("showapi_appid", "38643")
                    .params("showapi_sign", "f7258039023c40cfb4cfdb2870502d91")
                    .params("keyword", "薛之谦")
                    .params("page", "1")
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            songJsong = JSON.parseObject(response.body(), SongJsong.class);  //转化成Json类
                            //添加歌名
                            for (int i = 0; i < songJsong.getShowapi_res_body().getPagebean().getContentlist().size(); i++) {
                                list.add(songJsong.getShowapi_res_body().getPagebean().getContentlist().get(i).getSongname());
                                ListContentlist.getlistContentlist().setContentlistBean(songJsong.getShowapi_res_body().getPagebean().getContentlist().get(i));
                            }
                            songListAdpter.notifyDataSetChanged();
                        }
                    });
        }else {
            for (int i = 0; i < ListContentlist.getlistContentlist().getContentlistBeanSize(); i++) {
                list.add(ListContentlist.getlistContentlist().getContentlistBeans().get(i).getSongname());
            }
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
